<?php

namespace app\crm\model;


use think\Db;
use app\admin\model\Common;
use think\Request;
use think\Validate;

class Expence extends Common
{
    protected $name = 'crm_expence';
    protected $createTime = 'created_time';
    protected $updateTime = '';
    protected $autoWriteTimestamp = true;
    private $check_status=[0=>"待审",1=>"审批中",2=>"通过",3=>"失败",4=>"撤销"];
    /**
     * [getDataList 报销list]
     * @author Michael_xu
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     */
    /**
     * [getDataList 财务list]
     * @author Michael_xu
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     */
    public function getDataList($request)
    {
        unset($request["search"]);
        $request = $this->fmtRequest( $request );
        $map = $request['map'] ? : [];
        $user_id=$map["user_id"];
        unset($map["user_id"]);
        if(isset($map["customer_name"])){//客户名称
            $map["customer.name"]=["like","%".$map["customer_name"]["value"]."%"];
            unset($map["customer_name"]);
        }
        if(isset($map["order_sn"])){//客户名称
            $map["e.order_sn"]=["like","%".$map["order_sn"]["value"]."%"];
            unset($map["order_sn"]);
        }
        if(isset($map["finance_code"])){//财务编号
            $map["e.finance_code"]=["like","%".$map["finance_code"]["value"]."%"];
            unset($map["finance_code"]);
        }
        if(isset($map["department"])){//所属部门
            $map["e.department"]=["like","%".$map["department"]["value"]."%"];
            unset($map["department"]);
        }
        if(isset($map["user_name"])){//报销人
            $map["admin.realname"]=["like","%".$map["user_name"]["value"]."%"];
            unset($map["user_name"]);
        }
        if(isset($map["created_time"])){//报销时间查询
            $map["e.created_time"]=array(array('egt',date("Y-m-d H:i:s",$map["created_time"]["start"])),array('elt',date("Y-m-d H:i:s",$map["created_time"]["end"])),'and');;
            unset($map["created_time"]);
        }
        if(isset($map["status"])){//审核状态
            $check_status=["待审核"=>"0","通过"=>"2","审核中"=>"1","撤销"=>"4","失败"=>"3"];
            $map["e.status"]=$check_status[$map["status"]["value"]];
            unset($map["status"]);
        }
        //财务和超级管理员可以看所有，其他只能看对应自己和下级的  customer_name
        $isFinance=db("admin_access")->where(["user_id"=>$user_id,"group_id"=>9])->find();//是否是财务
        $isButler = db('admin_access')->where(["user_id"=>$user_id,"group_id"=>32])->find();//管家
        if(!$isButler && !$isFinance && $user_id!=1){
            $childIds=$this->getChildIds($user_id);
            $childIds[]=$user_id;
            $map["e.user_id"]=["in",$childIds];
        }
        
        $list = db('crm_expence')->alias('e')
            ->join('admin_user admin',"admin.id=e.user_id","left")
            ->join('__CRM_CUSTOMER__ customer',"customer.customer_id=e.customer_id","left")
            ->where($map)
            ->limit(($request['page']-1)*$request['limit'], $request['limit'])
            ->field('e.*,customer.name customer_name')
            ->order("e.id desc")
            ->select();
        if($list){
            foreach ($list as $k=>$v){
                $list[$k]["status"]=$this->check_status[$v["status"]];
            }
        }
        $dataCount =  db('crm_expence')->alias('e')
            ->join('admin_user admin',"admin.id=e.user_id","left")
            ->join('__CRM_CUSTOMER__ customer',"customer.customer_id=e.customer_id","left")
            ->where($map)
            ->count();
        $data = [];
        $data['list'] = $list;
        $data['dataCount'] = $dataCount ? : 0;
        return $data;
    }
    /**
     * @param $user_id
     * 获取用户的所有下级
     */
    public function getChildIds($user_id){
        $childsArr=db("admin_user")->where(["parent_id"=>$user_id])->column("id");
        if($childsArr){
            foreach ($childsArr as $v){
                $arr=$this->getChildIds($v);
                if($arr) $childsArr=array_merge($childsArr,$arr);
            }
        }
        return $childsArr;
    }
    public function add(){

    }

}